package com.tencent.wxcloudrun.controller;

import com.tencent.wxcloudrun.config.ApiResponse;
import com.tencent.wxcloudrun.constant.WxChatConstant;
import com.tencent.wxcloudrun.dto.WxSendTypeRequest;
import com.tencent.wxcloudrun.service.WxChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;

@RestController
public class WxGirlFriendController {

    // 上下文，用于策略模式获取对应策略
    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping(value = "/api/get")
    public ApiResponse sendOffWork_test() {
        System.out.println("test");
        try {
            // 参数一发送类型，参数二是推送的对象OpenId
            WxSendTypeRequest sendTypeRequest = new WxSendTypeRequest("OffWorkSend", WxChatConstant.openid_test, null);
            WxChatService chatService = applicationContext.getBean(sendTypeRequest.getType(), WxChatService.class);
            chatService.sendTest(sendTypeRequest);
        } catch (Exception e) {
            System.out.println("test" + e.getMessage());
            return ApiResponse.ok(e.getMessage());
        }
        return ApiResponse.ok("test");
    }

    // 表示每个月星期一到星期五下午4点50分执行
    @Scheduled(cron = "0 00 7 ? * MON-SUN")
    public void sendOffWork() throws ExecutionException, InterruptedException {
        System.out.println("开始发送早安");
        WxSendTypeRequest sendTypeRequest = new WxSendTypeRequest("OffWorkSend", WxChatConstant.openid_test, WxChatConstant.openid_tests);
        WxChatService chatService = applicationContext.getBean(sendTypeRequest.getType(), WxChatService.class);
        chatService.sendTest1(sendTypeRequest);
    }

    @Scheduled(cron = "0 30 11 ? * MON-SUN")
    public void sendOffWork2() throws ExecutionException, InterruptedException {
        System.out.println("开始发送午安");
        WxSendTypeRequest sendTypeRequest = new WxSendTypeRequest("OffWorkSend", WxChatConstant.openid_test, WxChatConstant.openid_tests);
        WxChatService chatService = applicationContext.getBean(sendTypeRequest.getType(), WxChatService.class);
        chatService.sendTest2(sendTypeRequest);
    }

    @Scheduled(cron = "0 30 17 ? * MON-SUN")
    public void sendOffWork3() throws ExecutionException, InterruptedException {
        System.out.println("开始发送晚饭");
        WxSendTypeRequest sendTypeRequest = new WxSendTypeRequest("OffWorkSend", WxChatConstant.openid_test, WxChatConstant.openid_tests);
        WxChatService chatService = applicationContext.getBean(sendTypeRequest.getType(), WxChatService.class);
        chatService.sendTest3(sendTypeRequest);
    }

    @Scheduled(cron = "0 30 22 ? * MON-SUN")
    public void sendOffWork4() throws ExecutionException, InterruptedException {
        System.out.println("开始发送晚安");
        WxSendTypeRequest sendTypeRequest = new WxSendTypeRequest("OffWorkSend", WxChatConstant.openid_test, WxChatConstant.openid_tests);
        WxChatService chatService = applicationContext.getBean(sendTypeRequest.getType(), WxChatService.class);
        chatService.sendTest4(sendTypeRequest);
    }

}
